package com.liy.framework.common.thread;

import java.util.concurrent.Executor;
import java.util.concurrent.ThreadPoolExecutor.CallerRunsPolicy;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;


@Configuration
@EnableAsync
public class TaskExecutePool {

    @Autowired
    private TaskThreadPoolConfig config;


    @Bean
    public Executor myTaskAsyncPool() {

        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();

        executor.setCorePoolSize(this.config.getCorePoolSize());
        executor.setMaxPoolSize(this.config.getMaxPoolSize());
        executor.setQueueCapacity(this.config.getQueueCapacity());
        executor.setKeepAliveSeconds(this.config.getKeepAliveSeconds());
        executor.setThreadNamePrefix("Das-Executor-");
        executor.setRejectedExecutionHandler(new CallerRunsPolicy());
        executor.initialize();

        return executor;
    }
}
